#ifdef _SYMBIAN_SDK_
#include "skyFrameWork.h"
#include "symWindow.h"
#include <avkon.hrh>

#include <e32std.h>
#include <e32base.h>
#include <f32file.h>
#include <etel.h>
#include <hal_data.h>
#include <CHARCONV.H>
#include <gdi.h>


#ifdef __SERIES60_3X__
	_LIT(KMyFontName,"Sans MT 936_S60");//
#else
	_LIT(KMyFontName,"CombinedChinesePlain12");//
#endif

void skyFrameWork::Draw( const ::TRect& aRect ) const
{
	CWindowGc& gc = SystemGc();
	if(m_bInited)
	{
		Painter* painter = (Painter*)app->getPainter();

		Sky::Rect rect;
		
		rect.x = aRect.iTl.iX;
		rect.y = aRect.iTl.iY;
		rect.w = aRect.Width();
		rect.h = aRect.Height();
		
		painter->setClipArea(0,0,240,320);
		painter->setInvalidArea(0,0,240,320);
		
		canvas.create(240,320,Canvas::Format_Default);
		painter->attachCanvas(&canvas);
		myWinMsg msg(&symWindow::handlePaint,vNull,painter);
		xhandleSendEvent(app,&msg);
		gc.BitBlt(aRect.iTl, canvas.getPict()->getBitmap() , aRect);
		painter->detachCanvas();
		canvas.Delete();
	}	
	
	return;
}
TKeyResponse skyFrameWork::OfferKeyEventL( const TKeyEvent &aKeyEvent, TEventCode aType )
{
	skyInput input;
	myAppMessagePtr p = skynew (myAppMessage(&symApp::handleInput,vNull,&input));
	if(input.convertInput(aKeyEvent,aType))
		Sky::xhandleSendEvent(app,p);
	return EKeyWasConsumed;
}

skyFrameWork* skyFrameWork::NewL( CAknAppUi& aAppUi )
{
	skyFrameWork* self = new (ELeave) skyFrameWork(aAppUi);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
}

Sky::MsgStatus	skyFrameWork::handleCallback(Framework::myEventManager* mgr,Framework::Message* message)
{
	if(message->isExactKindOf(Framework::myWindowletMessage::Class))
	{
		if(message->isEvent(&Framework::myWindowlet::handlePaint))
		{
			DrawNow();
		}
	}
	return vContinue;
}

skyFrameWork::skyFrameWork( CAknAppUi& aAppUi ):
iAppUi(aAppUi),
m_pFont(vNull)
{

}

void skyFrameWork::ConstructL()
{
	CreateWindowL();
	SetRect(iAppUi.ApplicationRect());

	//
	
	ActivateL();
}

skyFrameWork::~skyFrameWork()
{
	
}

void skyFrameWork::Initialise()
{
	app = xCreate();
	
	CGraphicsDevice* screenDevice =  CCoeEnv::Static()->ScreenDevice();
	CFont* pFont = vNull;
	i32 ff = FontMinWeight();
	TFontSpec itemFontSpec(KMyFontName, ff );
	itemFontSpec.iFontStyle = TFontStyle(EPostureUpright,EStrokeWeightBold,EPrintPosNormal);
	screenDevice->GetNearestFontInTwips(pFont,itemFontSpec);
		
	app->Initialise(Framework::Handler::Handle(this,&skyFrameWork::handleCallback));
	
	
	m_bInited = true;
	PainterPtr p = skynew (Painter());
	app->attachPainter(p);
}

int skyFrameWork::FontMinWeight()
{
	const CFont* font = 0;

	font = CEikonEnv::Static()->DenseFont();

	if (Rect().Height() <= 208 && Rect().Width() <= 176)
	{
		if(font)
		{
			return font->FontSpecInTwips().iHeight;
		}
	}

	//holyli 080412 modify beg 
	CGraphicsDevice * screenDevice = iCoeEnv->ScreenDevice() ;
	::TSize size = screenDevice->SizeInTwips(); 

	if (Rect().Height() == 320 && Rect().Width() == 240)
	{
		return 14*size.iHeight/Rect().Height();
	}
	if (Rect().Height() == 416 && Rect().Width() == 352)
	{
		return 18*size.iHeight/Rect().Height();
	}
	return 14*size.iHeight/Rect().Height();
}
#endif